///*
// *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
// *
// *  Redistribution and use in source and binary forms, with or without
// *  modification, are permitted provided that the following conditions are met:
// *
// *  Redistributions of source code must retain the above copyright notice,
// *  this list of conditions and the following disclaimer.
// *  Redistributions in binary form must reproduce the above copyright
// *  notice, this list of conditions and the following disclaimer in the
// *  documentation and/or other materials provided with the distribution.
// *  Neither the name of the dreamlu.net developer nor the names of its
// *  contributors may be used to endorse or promote products derived from
// *  this software without specific prior written permission.
// *  Author: Chill 庄骞 (smallchill@163.com)
// */
//package com.plian.system.service.dp.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.plian.Tools.*;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.entity.dp.DutyAnnualWorkPlan;
//import com.plian.system.mapper.dp.DutyAnnualWorkPlanMapper;
//import com.plian.system.mybatis.base.BaseFlowService;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.service.dp.IDutyAnnualWorkPlanService;
//import com.plian.system.service.sys.code.IFormCodeService;
//import com.plian.system.service.sys.file.FileService;
//import com.plian.system.service.wf.FormWorkflowService;
//import com.plian.system.vo.dp.DutyAnnualWorkPlanVO;
//import com.plian.system.wrapper.dp.DutyAnnualWorkPlanWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.stereotype.Service;
//
//import java.util.*;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//
///**
// * 责任追究 年度工作计划 服务实现类
// *
// * @author BladeX
// * @since 2021-05-07
// */
//@Slf4j
//@Service
//@AllArgsConstructor
//public class DutyAnnualWorkPlanServiceImpl extends BaseServiceImpl<DutyAnnualWorkPlanMapper, DutyAnnualWorkPlan> implements IDutyAnnualWorkPlanService, BaseFlowService {
//
//    private FileService fileService;
//
//    private FormWorkflowService formWorkflowService;
//
//    private IFormCodeService formCodeService;
//
//    private DutyAnnualWorkPlanWrapper dutyAnnualWorkPlanWrapper;
//
//    @Override
//    public boolean saveOrUpdate(DutyAnnualWorkPlan dutyAnnualWorkPlan) {
//        boolean flag = false;
//        if (dutyAnnualWorkPlan.getId() == null){
//            flag = true;
//            dutyAnnualWorkPlan.setCode(formCodeService.getCode(FormTypeConstant.DP_DUTY_ANNUAL_WORK_PLAN_FORM));
//            dutyAnnualWorkPlan.setFormType(String.valueOf(FormTypeConstant.DP_DUTY_ANNUAL_WORK_PLAN_FORM));
//        }
//
//        boolean result = super.saveOrUpdate(dutyAnnualWorkPlan);
//        if (flag){
//            fileService.saveAll(String.valueOf(dutyAnnualWorkPlan.getId()), dutyAnnualWorkPlan.getDocList());
//        }else {
//            fileService.edit(String.valueOf(dutyAnnualWorkPlan.getId()), dutyAnnualWorkPlan.getDocList());
//        }
//
//        return result;
//    }
//
//    @Override
//    public boolean deleteLogic(List<Long> ids) {
//        for (Long id : ids) {
//            DutyAnnualWorkPlan dutyAnnualWorkPlan = getById(id);
//            if (dutyAnnualWorkPlan == null) {
//                return false;
//            } else if (dutyAnnualWorkPlan.getStatus() != null &&
//                    (dutyAnnualWorkPlan.getStatus() != FormStatusConstant.STASH && dutyAnnualWorkPlan.getStatus() != FormStatusConstant.UNREVIEWED)) {
//                return false;
//            }
//            formWorkflowService.deleteProcessInstance(String.valueOf(id));
//        }
//        boolean result = super.deleteLogic(ids);
//        return result;
//    }
//
//    @Override
//    public HashMap<String,Object> commit(String id) {
//        handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//
//        //将需要提交的流程变量返回，用于工作流侧
//        return new HashMap<String,Object>();
//    }
//
//    @Override
//    public Boolean finish(String id) {
//        /**
//         * 设置审批人和审批时间
//         */
//        DutyAnnualWorkPlan dutyAnnualWorkPlan = getById(id);
//        dutyAnnualWorkPlan.setApprovedUser(TokenUtil.getTokenUserId());
//        Date now = DateUtil.now();
//        dutyAnnualWorkPlan.setApprovedTime(now);
//        updateById(dutyAnnualWorkPlan);
//
//        return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//    }
//
//    @Override
//    public Boolean reject(String id, int formState, String comment) {
//        return handleStatus(Long.valueOf(id), formState);
//    }
//
//    @Override
//    public List<PageData> findByIds(List<String> formIds) {
//        QueryWrapper<DutyAnnualWorkPlan> queryWrapper = new QueryWrapper<>();
//        queryWrapper.in("id", Func.toLongList(StringUtils.join(formIds, StringPool.COMMA)));
//        List<DutyAnnualWorkPlan> annualWorkPlans = list(queryWrapper);
//        List<DutyAnnualWorkPlanVO> dutyAnnualWorkPlanVOS = dutyAnnualWorkPlanWrapper.entityToVO(annualWorkPlans);
//        List<PageData> pageDataList = new ArrayList<>();
//        try{
//            if (CollectionUtil.isNotEmpty(dutyAnnualWorkPlanVOS)){
//                for (DutyAnnualWorkPlanVO vo : dutyAnnualWorkPlanVOS){
//                    PageData pd = new PageData();
//                    pd.put("id", vo.getId());
//                    pd.put("UNITNAME", vo.getReportCompanyName());
//                    pd.put("APPLICANTNAME", vo.getCreateUserName());
//                    pd.put("applicantId", vo.getCreateUser());
//                    pd.put("applyOrganizationId", vo.getReportCompanyId());
//                    pd.put("formType", FormTypeConstant.DP_DUTY_ANNUAL_WORK_PLAN_FORM);
//                    pd.put("orgName", vo.getReportCompanyName());
//                    pageDataList.add(pd);
//                }
//            }
//
//            return pageDataList;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//        }
//        return pageDataList;
//    }
//
//    @Override
//    public List<Integer> getFormType() {
//        return Arrays.asList(FormTypeConstant.DP_DUTY_ANNUAL_WORK_PLAN_FORM);
//    }
//
//    @Override
//    public void goBackToStash(String formId) {
//        handleStatus(Long.valueOf(formId), UNREVIEWED);
//    }
//
//    /**
//     * 统一处理状态机事件方法
//     * @param id 主键
//     * @param updateStatus 更新状态
//     * @return boolean
//     */
//    private Boolean handleStatus(Long id, int updateStatus) {
//        try {
//            DutyAnnualWorkPlan dutyAnnualWorkPlan = getById(id);
//            dutyAnnualWorkPlan.setStatus(updateStatus);
//            updateById(dutyAnnualWorkPlan);
//            return true;
//        } catch (Exception e) {
//           log.error(ExceptionUtils.getFullStackTrace(e));
//            return false;
//        }
//    }
//
//    @Override
//    public boolean submit(DutyAnnualWorkPlan dutyAnnualWorkPlan) {
//        if (!Optional.ofNullable(dutyAnnualWorkPlan.getStatus()).isPresent()){
//            dutyAnnualWorkPlan.setStatus(FormStatusConstant.UNREVIEWED);
//        }
//
//        boolean result = saveOrUpdate(dutyAnnualWorkPlan);
//        HashMap<String, Object> processVariable = new HashMap<>();
//        formWorkflowService.startProcessAndSimulate(String.valueOf(dutyAnnualWorkPlan.getId()),dutyAnnualWorkPlan.getFormType(),processVariable);
//        return result;
//    }
//}
